import os

from openpyxl import load_workbook


def read(file_name):
    # 传测试用例文件绝对路径
    # mac
    # sheet_name = file_name.split("/")[-1].split(".")[0]
    # win
    sheet_name = file_name.split("\\")[-1].split(".")[0]

    base_path = os.path.dirname(__file__)
    print(base_path)
    new_path = base_path.replace('utils', 'resource\\test_data.xlsx')
    excel = load_workbook(new_path)
    sheet = excel[sheet_name]
    table = []
    date_colum = 0
    verify_date_colum = 0
    # 按行遍历，每次打印出一行
    for row in sheet:
        line = []
        # 遍历单元格
        for cell in row:
            if cell.row == 1 and cell.value == 'date':
                # 取第几列
                date_colum = cell.column
            if cell.row == 1 and cell.value == 'verify_date':
                verify_date_colum = cell.column
            if cell.row != 1 and cell.column == date_colum:
                # xxxx/x/x x:x
                cell.value = cell.value.strftime("%Y/%m/%d %H:%M")
            if cell.row != 1 and cell.column == verify_date_colum:
                # Mar 3, 2000
                # cell.value = cell.value.strftime("%b %d, %Y")
                # 如何去掉前导的0
                cell.value = cell.value.strftime('%b') + ' ' + str(
                    int(cell.value.strftime('%d'))) + ', ' + cell.value.strftime('%Y')

            line.append(cell.value)
        table.append(line)
    print(table[1:])
    # 返回第二行以后
    return table[1:]


# 'if __name_- =='__main__':的作用，其他文件import这个文件文件时，这段代码不会自动执行否则
# 如果没有'if__name__ =='__main__'，要么在当前文件的方法read()不能被执行
if __name__ == '__main__':
    read(r"D:\Download\pycharm\pythonProject\auto-fun-test\test_cases\test_login4.py")
    # read(r"/Users/lijunlong/Desktop/PythonProject/auto-fun-test/test_cases/test_login4.py")
